package thermologic.gui;

import thermologic.core.Channel;
import thermologic.core.Resolution;

/**
 * UI Component for show and modify a channel
 * object.
 * 
 * @author  mrivadeneira
 */
public class ChannelTab extends javax.swing.JPanel {

    /**
     * Channel is being viewed or modified
     */
    private Channel channel;
    
    /** Creates new form ChannelTab */
    public ChannelTab() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        channelEnnabled = new javax.swing.JCheckBox();
        sampleRate = new javax.swing.JSpinner();
        resolution = new javax.swing.JComboBox();
        alarmLow = new javax.swing.JSpinner();
        alarmHigh = new javax.swing.JSpinner();
        sampleRateLabel = new javax.swing.JLabel();
        resolutionLabel = new javax.swing.JLabel();
        alarmLowLabel = new javax.swing.JLabel();
        alarmHighLabel = new javax.swing.JLabel();
        sampleRateUnit = new javax.swing.JLabel();
        resolutionUnit = new javax.swing.JLabel();
        alarmLowUnit = new javax.swing.JLabel();
        alarmHighUnit = new javax.swing.JLabel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("properties/language"); // NOI18N
        channelEnnabled.setText(bundle.getString("thermologic.setupdialog.enabledChannel")); // NOI18N

        sampleRateLabel.setText(bundle.getString("thermologic.setupdialog.sampleRateLabel")); // NOI18N

        resolutionLabel.setText(bundle.getString("thermologic.configTable.param7")); // NOI18N

        alarmLowLabel.setText(bundle.getString("thermologic.configTable.param5")); // NOI18N

        alarmHighLabel.setText(bundle.getString("thermologic.configTable.param6")); // NOI18N

        sampleRateUnit.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(channelEnnabled)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(alarmLowLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                            .addComponent(alarmHighLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(resolutionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(sampleRateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sampleRate, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                            .addComponent(alarmHigh, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                            .addComponent(alarmLow, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                            .addComponent(resolution, 0, 56, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sampleRateUnit)
                    .addComponent(resolutionUnit)
                    .addComponent(alarmLowUnit)
                    .addComponent(alarmHighUnit))
                .addGap(141, 141, 141))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(channelEnnabled)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sampleRateLabel)
                    .addComponent(sampleRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sampleRateUnit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(resolution, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(resolutionLabel)
                    .addComponent(resolutionUnit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(alarmLow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alarmLowLabel)
                    .addComponent(alarmLowUnit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(alarmHigh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alarmHighLabel)
                    .addComponent(alarmHighUnit))
                .addContainerGap(18, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
        
        channelEnnabled.setSelected(getChannel().isChannelEnnabled());
        sampleRate.setValue(getChannel().getSampleRate());
        alarmLow.setValue(getChannel().getAlarmLow());
        alarmHigh.setValue(getChannel().getAlarmHigh());
        
        sampleRateUnit.setText(channel.getSampleRateUnit());
        resolutionUnit.setText(channel.getUnit());
        alarmLowUnit.setText(channel.getUnit());
        alarmHighUnit.setText(channel.getUnit());
        
        resolution.removeAllItems();
        
        for(Resolution res: channel.getAvailableResolutions())
        {
            resolution.addItem(res);
        }
        resolution.setSelectedItem(channel.getResolution());
    } 
    
    /**
     * Transfer the values from UI components
     * to channel object
     */
    public void updateChannel()
    {
        channel.setChannelEnnabled(channelEnnabled.isSelected());
        channel.setSampleRate(Double.parseDouble(sampleRate.getValue().toString()));
        channel.setAlarmLow(Double.parseDouble(alarmLow.getValue().toString()));
        channel.setAlarmHigh(Double.parseDouble(alarmHigh.getValue().toString()));
        channel.setResolution((Resolution)resolution.getSelectedItem());
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner alarmHigh;
    private javax.swing.JLabel alarmHighLabel;
    private javax.swing.JLabel alarmHighUnit;
    private javax.swing.JSpinner alarmLow;
    private javax.swing.JLabel alarmLowLabel;
    private javax.swing.JLabel alarmLowUnit;
    private javax.swing.JCheckBox channelEnnabled;
    private javax.swing.JComboBox resolution;
    private javax.swing.JLabel resolutionLabel;
    private javax.swing.JLabel resolutionUnit;
    private javax.swing.JSpinner sampleRate;
    private javax.swing.JLabel sampleRateLabel;
    private javax.swing.JLabel sampleRateUnit;
    // End of variables declaration//GEN-END:variables

    
}
